package com.a3dweijing.wetouch2.network;

/**
 * Created by Administrator on 2018/4/8.
 */
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Toast;

import com.a3dweijing.wetouch2.Activity.ShoppingDetailsActivity;
import com.a3dweijing.wetouch2.Common.CallBack;

import java.util.ArrayList;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class CommObserver<T> implements Observer<T>
{
    private View view1, view2;
    
    private ArrayList<Disposable> ds;
    
    private CallBack callBack;
    
    private AlertDialog dialog;
    
    private Context context;
    
    public CommObserver(@Nullable AlertDialog dialog, @Nullable View view1, @Nullable View view2,
        @Nullable ArrayList<Disposable> ds, CallBack callBack, Context context)
    {
        this.context=context;
        this.view1 = view1;
        this.view2 = view2;
        this.ds = ds;
        this.callBack = callBack;
        this.dialog = dialog;
        if (dialog != null)
            dialog.show();
        if (view1 != null)
            view1.setVisibility(View.VISIBLE);
        if (view2 != null)
            view2.setVisibility(View.GONE);
    }
    
    @Override
    public void onSubscribe(Disposable d)
    {
        if (ds != null)
        {
            ds.add(d);
        }
    }
    
    @Override
    public void onNext(T o)
    {
        callBack.doNext(0, o);
    }
    
    @Override
    public void onError(Throwable e)
    {
        if (view1 != null)
            view1.setVisibility(View.GONE);
        if (view2 != null)
            view2.setVisibility(View.VISIBLE);
        if (dialog != null)
            dialog.dismiss();
        Toast.makeText(context, "网络错误...", Toast.LENGTH_SHORT).show();
    }
    
    @Override
    public void onComplete()
    {
        if (view1 != null)
            view1.setVisibility(View.GONE);
        if (dialog != null)
            dialog.dismiss();
    }
    
}
